<?php

namespace App\Models;

use App\Models\Finance\Address;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use App\Models\Finance\Wallet;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use App\Models\Finance\Withdraw;

class User extends Authenticatable
{
    use Notifiable;
    use HasDateTimeFormatter;

    protected $table = 'yida_user';

    /**
     * 角色：管理员
     */
    const ROLE_ADMIN = 'Admin';

    /**
     * 性别：男
     */
    const GENDER_MALE = 'Male';

    /**
     * 性别：女
     */
    const GENDER_FEMALE = 'Female';



    protected $fillable = [
        'username',
        'country_code',
        'mobile',
        'email',
        'password',
        'mobile_verified',
        'parent_id',
        'nickname',
        'gender',
        'birthday',
        'club',
        'secondary_password',
        'is_private',
        'avatar',
        'app_id',
        'is_tip',
        'is_msg',
        'cid',
        'type',
        'grade_id'
    ];

    //假如需要显示的字段
    protected $visible = [
        'id',
        'username',
        'created_at',
        'country_code',
        'trust_count',
        'trade_count',
        'lend_trade_count',
        'rating',
        'loanTotalAmount',
        'vague_volume',
        'avatar',
        'online',
        'has_google2fa',
        'role',
        'nickname',
        'cid',
        'type',
        'grade_id',
        'status',
        'pid',
        'wallet_status'
    ];

    protected $casts = [
        'authentication_id' => 'integer',
        'email_verified' => 'boolean',
        'mobile_verified' => 'boolean',
        'avatar' => 'array',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'created_at' => 'datetime:Y-m-d H:i:s',
        'banned_to' => 'datetime:Y-m-d H:i:s',
        'ban_purchase_to' => 'datetime:Y-m-d H:i:s',
        'register_ips' => 'collection',
        'is_private' => 'boolean'
    ];

    protected $appends = [
        'vague_email',
//        'loanTotalAmount',
        'disabled',
        'buy_disabled',
        'loan_disabled',
        'wallet_disabled',
        'user_grade',
        'total_count'
//        'sales_total',
//        'sales_day'
    ];
    public static function boot()
    {
        parent::boot();

        static::created(function ($user){
            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_insert'=>$user->toArray()];
            Log::channel("LaravelInsert")->error("添加操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"insert",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "添加操作",
                'table' => "yida_user"
            ]);
        });
        static::updated(function ($user){
            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_update'=>$user->toArray()];
            Log::channel("LaravelUpdate")->error("更新操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"update",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "更新操作",
                'table' => "yida_user"
            ]);
        });
        static::deleted(function ($user){

            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_delete'=>$user->toArray()];
            Log::channel("LaravelDelete")->error("删除操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"delete",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "删除操作",
                'table' => "yida_user"
            ]);
        });
    }


}
